const storage = {};
Object.defineProperties(storage, {
  token: {
    enumerable: true,
    get() {
      const cookies = document.cookie.split(/;\s*/g);
      let cookieItem;
      while ((cookieItem = cookies.shift())) {
        if (cookieItem.indexOf('token') === 0) {
          return cookieItem.split('=')[1];
        }
      }
      return null;
    },
    set(val) {
      // let exp = new Date();
      // exp.setTime(exp.getTime() + 1000 * 60 * 60 * 24); // 过期时间1天
      document.cookie = `token=${val};path=/`;
    },
  },

  userInfo: {
    enumerable: true,
    get() {
      if (localStorage.userInfo !== undefined) {
        return JSON.parse(localStorage.userInfo);
      } else {
        return {};
      }
    },
    set(val) {
      localStorage.userInfo = JSON.stringify(val);
    },
  },

  dept: {
    enumerable: true,
    get() {
      if (localStorage.dept !== undefined) {
        return JSON.parse(localStorage.dept);
      } else {
        return {};
      }
    },
    set(val) {
      localStorage.dept = JSON.stringify(val);
    },
  },

  strategyExport: {
    enumerable: true,
    get() {
      if (sessionStorage.strategyExport !== undefined) {
        return JSON.parse(sessionStorage.strategyExport);
      } else {
        return {};
      }
    },
    set(val) {
      sessionStorage.strategyExport = JSON.stringify(val);
    },
  },
  templateToStrategyId: {
    enumerable: true,
    get() {
      if (sessionStorage.templateToStrategyId !== undefined) {
        return JSON.parse(sessionStorage.templateToStrategyId);
      } else {
        return '';
      }
    },
    set(val) {
      sessionStorage.templateToStrategyId = JSON.stringify(val);
    },
  },
  onlineSourceId: {
    enumerable: true,
    get() {
      if (sessionStorage.onlineSourceId !== undefined) {
        return JSON.parse(sessionStorage.onlineSourceId);
      } else {
        return '';
      }
    },
    set(val) {
      sessionStorage.onlineSourceId = JSON.stringify(val);
    },
  },
  strategyExportMsg: {
    enumerable: true,
    get() {
      if (sessionStorage.strategyExportMsg !== undefined) {
        return JSON.parse(sessionStorage.strategyExportMsg);
      } else {
        return {};
      }
    },
    set(val) {
      sessionStorage.strategyExportMsg = JSON.stringify(val);
    },
  },
  HitDetail: {
    enumerable: true,
    get() {
      if (sessionStorage.HitDetail !== undefined) {
        return JSON.parse(sessionStorage.HitDetail);
      } else {
        return {};
      }
    },
    set(val) {
      sessionStorage.HitDetail = JSON.stringify(val);
    },
  },
  org: {
    enumerable: true,
    get() {
      if (localStorage.org !== undefined) {
        return JSON.parse(localStorage.org);
      } else {
        return {};
      }
    },
    set(val) {
      localStorage.org = JSON.stringify(val);
    },
  },
  clear: {
    value() {
      const temp = new Date();
      temp.setTime(temp.getTime() - 100);
      document.cookie = `token='';expires=${temp.toGMTString()};path=/`;
      localStorage.removeItem('userInfo');
      localStorage.removeItem('currentWS');
      localStorage.removeItem('config');
      localStorage.removeItem('dept');
    },
  },
});

export default storage;
